﻿namespace OleDbDAL.Company
{
    using DBUtility;
    using IDal.Company;
    using System;
    using System.Data;
    using System.Text;

    public class ProfitAndLossApp : IProfitAndLoss
    {
        private string dbname = "Systems";

        public string ReturnInventoryInput(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(ProfitMoney) as ProfitMoney from T_Inventory,T_InventoryComm where T_Inventory.InventoryId=T_InventoryComm.InventoryId and T_Inventory.ChainNum=" + ChainNum + " and T_InventoryComm.ProfitMoney>0");
            builder.Append(" and T_Inventory.InventTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["ProfitMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnInventoryOut(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(ProfitMoney) as ProfitMoney from T_Inventory,T_InventoryComm where T_Inventory.InventoryId=T_InventoryComm.InventoryId and T_Inventory.ChainNum=" + ChainNum + " and T_InventoryComm.ProfitMoney<0");
            builder.Append(" and T_Inventory.InventTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["ProfitMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnOtherInput(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(BuildMoney) as BuildMoney from T_CompayAccountDetails ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and IsCheck=1");
            builder.Append(" and DetailsType=0");
            builder.Append(" and CheckTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["BuildMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnOtherOut(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(BuildMoney) as BuildMoney from T_CompayAccountDetails ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and IsCheck=1");
            builder.Append(" and DetailsType=1");
            builder.Append(" and CheckTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["BuildMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnProcurment(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(ToMeetMoney) as ToMeetMoney from T_Procurement ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and IsInput=1");
            builder.Append(" and IsBlackOut=0");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["ToMeetMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnSaleCost(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(CostMoney) as CostMoney from T_Orders ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and IsOut=1");
            builder.Append(" and IsBlackOut=0");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["CostMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnSaleMoney(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            string str2 = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(ToMeetMoney) as PaidMoney from T_Orders ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and SaleVtag=2");
            builder.Append(" and IsOut=1");
            builder.Append(" and IsBlackOut=0");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["PaidMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            builder = new StringBuilder();
            builder.Append("select sum(ToMeetMoney) as PaidMoney from T_Orders ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and SaleVtag<>2");
            builder.Append(" and IsOut=1");
            builder.Append(" and IsBlackOut=0");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str2 = table.Rows[0]["PaidMoney"].ToString();
                if (str2 == "")
                {
                    str2 = "0.00";
                }
            }
            return (str + "|" + str2);
        }

        public string ReturnSaleReturnMoney(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(ReturnMoney) as ReturnMoney from T_Orders ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and SaleVtag=2");
            builder.Append(" and IsUseReturn=1");
            builder.Append(" and IsBlackOut=0");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["ReturnMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnSendProMoney(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(SentProMoney) as SentProMoney from T_Orders ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and IsOut=1");
            builder.Append(" and IsBlackOut=0");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["SentProMoney"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }

        public string ReturnTestDunk(DateTime beginTime, DateTime endTime, string ChainNum)
        {
            string str = "0.00";
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(Prices) as Prices from T_TestDunk ");
            builder.Append(" where chainNum=" + ChainNum);
            builder.Append(" and IsCheck=1");
            builder.Append(" and CreateTime Between #" + beginTime.ToString("yyyy-MM-dd 00:00:00") + "# and #" + endTime.ToString("yyyy-MM-dd 23:59:59") + "#");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["Prices"].ToString();
                if (str == "")
                {
                    str = "0.00";
                }
            }
            return str;
        }
    }
}

